package com.neusoft.databus.core.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.databus.common.enums.MetricUsageType;
import com.neusoft.databus.common.model.MetricGroup;
import com.neusoft.databus.common.model.QMetric;
import com.neusoft.databus.common.model.QMetricGroup;
import com.neusoft.databus.common.model.QResource;
import com.neusoft.databus.core.repository.CustomizedMetricGroupRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedMetricGroupRepositoryImpl extends AbstractRepository implements CustomizedMetricGroupRepository {

    @Override
    public Page<MetricGroup> search(Pageable pageable, Searchable searchable) {
        final QMetricGroup qMetricGroup = QMetricGroup.metricGroup;
        final JPAQuery<MetricGroup> query = this.query().selectFrom(qMetricGroup);
        final BooleanBuilder where = this.buildWhere(query, searchable);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    public List<MetricGroup> search(Searchable searchable) {
        final QMetricGroup qMetricGroup = QMetricGroup.metricGroup;
        final JPAQuery<MetricGroup> query = this.query().selectFrom(qMetricGroup);
        final BooleanBuilder where = this.buildWhere(query, searchable);
        query.where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return MetricGroup.class;
    }

    private BooleanBuilder buildWhere(JPAQuery<MetricGroup> query, Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QMetricGroup qMetricGroup = QMetricGroup.metricGroup;
        final QMetric qMetric = QMetric.metric;
        final QResource qResource = QResource.resource;
        where.and(super.containsStr(qMetricGroup.name, searchable, "name"));
        where.and(super.containsStr(qMetricGroup.code, searchable, "code"));
        where.and(super.equalsEnum(qMetricGroup.usageType, searchable, "usageType", (x) -> MetricUsageType.valueOf(x)));
        //where.and(super.equalsLong(qMetricGroup.executionCycle, searchable, "executionCycle"));

        if (searchable.hasKey("metric") && (null != searchable.getStrValue("metric"))) {
            final BooleanBuilder orMetric = new BooleanBuilder();
            orMetric.or(qMetric.code.containsIgnoreCase(searchable.getStrValue("metric")))
                    .or(qMetric.name.containsIgnoreCase(searchable.getStrValue("metric")))
                    .or(qMetric.variable.containsIgnoreCase(searchable.getStrValue("metric")));

            where.and(qMetricGroup.id
                    .in(JPAExpressions.select(qMetricGroup.id).from(qMetricGroup)
                            .leftJoin(qMetricGroup.metrics, qMetric)
                            .where(orMetric)));
        }
        if (searchable.hasKey("resource") && (null != searchable.getStrValue("resource"))) {
            final BooleanBuilder orResource = new BooleanBuilder();
            orResource.or(qResource.name.containsIgnoreCase(searchable.getStrValue("resource")))
                    .or(qResource.code.containsIgnoreCase(searchable.getStrValue("resource")));
            where.and(qMetricGroup.id
                    .in(JPAExpressions.select(qMetricGroup.id).from(qResource)
                            .leftJoin(qResource.metricGroups, qMetricGroup)
                            .where(orResource)));
        }
        return where;
    }

}
